<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="年份" prop="year">
        <el-input
          v-model="queryParams.year"
          placeholder="请输入年份"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="月份(1-12)" prop="month">
        <el-input
          v-model="queryParams.month"
          placeholder="请输入月份(1-12)"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总项目数" prop="totalProjects">
        <el-input
          v-model="queryParams.totalProjects"
          placeholder="请输入总项目数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总客户数" prop="totalCustomer">
        <el-input
          v-model="queryParams.totalCustomer"
          placeholder="请输入总客户数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总供应商数" prop="totalSupplier">
        <el-input
          v-model="queryParams.totalSupplier"
          placeholder="请输入总供应商数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总活跃客户数" prop="totalActiveCustomer">
        <el-input
          v-model="queryParams.totalActiveCustomer"
          placeholder="请输入总活跃客户数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总活跃供应商数" prop="totalActiveSupplier">
        <el-input
          v-model="queryParams.totalActiveSupplier"
          placeholder="请输入总活跃供应商数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户总订单数" prop="totalCustomerOrders">
        <el-input
          v-model="queryParams.totalCustomerOrders"
          placeholder="请输入客户总订单数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商总订单数" prop="totalSupplierOrders">
        <el-input
          v-model="queryParams.totalSupplierOrders"
          placeholder="请输入供应商总订单数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="未按时交货订单数" prop="notOnTimeDeliveryOrders">
        <el-input
          v-model="queryParams.notOnTimeDeliveryOrders"
          placeholder="请输入未按时交货订单数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="按时交货订单率(%)" prop="onTimeDeliveryRate">
        <el-input
          v-model="queryParams.onTimeDeliveryRate"
          placeholder="请输入按时交货订单率(%)"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户总采购金额" prop="totalCustomerAmount">
        <el-input
          v-model="queryParams.totalCustomerAmount"
          placeholder="请输入客户总采购金额"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商总采购金额" prop="totalSupplierAmount">
        <el-input
          v-model="queryParams.totalSupplierAmount"
          placeholder="请输入供应商总采购金额"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户产品总量" prop="totalCustomerQuantity">
        <el-input
          v-model="queryParams.totalCustomerQuantity"
          placeholder="请输入客户产品总量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商产品总量" prop="totalSupplierQuantity">
        <el-input
          v-model="queryParams.totalSupplierQuantity"
          placeholder="请输入供应商产品总量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="不合格产品总量" prop="totalCustomerDefectiveQuantity">
        <el-input
          v-model="queryParams.totalCustomerDefectiveQuantity"
          placeholder="请输入不合格产品总量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="不合格产品总量" prop="totalSupplierDefectiveQuantity">
        <el-input
          v-model="queryParams.totalSupplierDefectiveQuantity"
          placeholder="请输入不合格产品总量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入库总量" prop="totalStockIn">
        <el-input
          v-model="queryParams.totalStockIn"
          placeholder="请输入入库总量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出库总量" prop="totalStockOut">
        <el-input
          v-model="queryParams.totalStockOut"
          placeholder="请输入出库总量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产良率(%)" prop="productionYieldRate">
        <el-input
          v-model="queryParams.productionYieldRate"
          placeholder="请输入生产良率(%)"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['scm:scmStatsMonthly:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['scm:scmStatsMonthly:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['scm:scmStatsMonthly:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['scm:scmStatsMonthly:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="scmStatsMonthlyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="统计ID" align="center" prop="id" />
      <el-table-column label="年份" align="center" prop="year" />
      <el-table-column label="月份(1-12)" align="center" prop="month" />
      <el-table-column label="总项目数" align="center" prop="totalProjects" />
      <el-table-column label="总客户数" align="center" prop="totalCustomer" />
      <el-table-column label="总供应商数" align="center" prop="totalSupplier" />
      <el-table-column label="总活跃客户数" align="center" prop="totalActiveCustomer" />
      <el-table-column label="总活跃供应商数" align="center" prop="totalActiveSupplier" />
      <el-table-column label="客户总订单数" align="center" prop="totalCustomerOrders" />
      <el-table-column label="供应商总订单数" align="center" prop="totalSupplierOrders" />
      <el-table-column label="未按时交货订单数" align="center" prop="notOnTimeDeliveryOrders" />
      <el-table-column label="按时交货订单率(%)" align="center" prop="onTimeDeliveryRate" />
      <el-table-column label="客户总采购金额" align="center" prop="totalCustomerAmount" />
      <el-table-column label="供应商总采购金额" align="center" prop="totalSupplierAmount" />
      <el-table-column label="客户产品总量" align="center" prop="totalCustomerQuantity" />
      <el-table-column label="供应商产品总量" align="center" prop="totalSupplierQuantity" />
      <el-table-column label="不合格产品总量" align="center" prop="totalCustomerDefectiveQuantity" />
      <el-table-column label="不合格产品总量" align="center" prop="totalSupplierDefectiveQuantity" />
      <el-table-column label="入库总量" align="center" prop="totalStockIn" />
      <el-table-column label="出库总量" align="center" prop="totalStockOut" />
      <el-table-column label="生产良率(%)" align="center" prop="productionYieldRate" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['scm:scmStatsMonthly:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['scm:scmStatsMonthly:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改月度统计信息-总对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="scmStatsMonthlyRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="年份" prop="year">
          <el-input v-model="form.year" placeholder="请输入年份" />
        </el-form-item>
        <el-form-item label="月份(1-12)" prop="month">
          <el-input v-model="form.month" placeholder="请输入月份(1-12)" />
        </el-form-item>
        <el-form-item label="总项目数" prop="totalProjects">
          <el-input v-model="form.totalProjects" placeholder="请输入总项目数" />
        </el-form-item>
        <el-form-item label="总客户数" prop="totalCustomer">
          <el-input v-model="form.totalCustomer" placeholder="请输入总客户数" />
        </el-form-item>
        <el-form-item label="总供应商数" prop="totalSupplier">
          <el-input v-model="form.totalSupplier" placeholder="请输入总供应商数" />
        </el-form-item>
        <el-form-item label="总活跃客户数" prop="totalActiveCustomer">
          <el-input v-model="form.totalActiveCustomer" placeholder="请输入总活跃客户数" />
        </el-form-item>
        <el-form-item label="总活跃供应商数" prop="totalActiveSupplier">
          <el-input v-model="form.totalActiveSupplier" placeholder="请输入总活跃供应商数" />
        </el-form-item>
        <el-form-item label="客户总订单数" prop="totalCustomerOrders">
          <el-input v-model="form.totalCustomerOrders" placeholder="请输入客户总订单数" />
        </el-form-item>
        <el-form-item label="供应商总订单数" prop="totalSupplierOrders">
          <el-input v-model="form.totalSupplierOrders" placeholder="请输入供应商总订单数" />
        </el-form-item>
        <el-form-item label="未按时交货订单数" prop="notOnTimeDeliveryOrders">
          <el-input v-model="form.notOnTimeDeliveryOrders" placeholder="请输入未按时交货订单数" />
        </el-form-item>
        <el-form-item label="按时交货订单率(%)" prop="onTimeDeliveryRate">
          <el-input v-model="form.onTimeDeliveryRate" placeholder="请输入按时交货订单率(%)" />
        </el-form-item>
        <el-form-item label="客户总采购金额" prop="totalCustomerAmount">
          <el-input v-model="form.totalCustomerAmount" placeholder="请输入客户总采购金额" />
        </el-form-item>
        <el-form-item label="供应商总采购金额" prop="totalSupplierAmount">
          <el-input v-model="form.totalSupplierAmount" placeholder="请输入供应商总采购金额" />
        </el-form-item>
        <el-form-item label="客户产品总量" prop="totalCustomerQuantity">
          <el-input v-model="form.totalCustomerQuantity" placeholder="请输入客户产品总量" />
        </el-form-item>
        <el-form-item label="供应商产品总量" prop="totalSupplierQuantity">
          <el-input v-model="form.totalSupplierQuantity" placeholder="请输入供应商产品总量" />
        </el-form-item>
        <el-form-item label="不合格产品总量" prop="totalCustomerDefectiveQuantity">
          <el-input v-model="form.totalCustomerDefectiveQuantity" placeholder="请输入不合格产品总量" />
        </el-form-item>
        <el-form-item label="不合格产品总量" prop="totalSupplierDefectiveQuantity">
          <el-input v-model="form.totalSupplierDefectiveQuantity" placeholder="请输入不合格产品总量" />
        </el-form-item>
        <el-form-item label="入库总量" prop="totalStockIn">
          <el-input v-model="form.totalStockIn" placeholder="请输入入库总量" />
        </el-form-item>
        <el-form-item label="出库总量" prop="totalStockOut">
          <el-input v-model="form.totalStockOut" placeholder="请输入出库总量" />
        </el-form-item>
        <el-form-item label="生产良率(%)" prop="productionYieldRate">
          <el-input v-model="form.productionYieldRate" placeholder="请输入生产良率(%)" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ScmStatsMonthly">
import { listScmStatsMonthly, getScmStatsMonthly, delScmStatsMonthly, addScmStatsMonthly, updateScmStatsMonthly } from "@/api/scm/scmStatsMonthly";

const { proxy } = getCurrentInstance();

const scmStatsMonthlyList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    year: null,
    month: null,
    totalProjects: null,
    totalCustomer: null,
    totalSupplier: null,
    totalActiveCustomer: null,
    totalActiveSupplier: null,
    totalCustomerOrders: null,
    totalSupplierOrders: null,
    notOnTimeDeliveryOrders: null,
    onTimeDeliveryRate: null,
    totalCustomerAmount: null,
    totalSupplierAmount: null,
    totalCustomerQuantity: null,
    totalSupplierQuantity: null,
    totalCustomerDefectiveQuantity: null,
    totalSupplierDefectiveQuantity: null,
    totalStockIn: null,
    totalStockOut: null,
    productionYieldRate: null,
  },
  rules: {
    year: [
      { required: true, message: "年份不能为空", trigger: "blur" }
    ],
    month: [
      { required: true, message: "月份(1-12)不能为空", trigger: "blur" }
    ],
    totalProjects: [
      { required: true, message: "总项目数不能为空", trigger: "blur" }
    ],
    totalCustomer: [
      { required: true, message: "总客户数不能为空", trigger: "blur" }
    ],
    totalSupplier: [
      { required: true, message: "总供应商数不能为空", trigger: "blur" }
    ],
    totalActiveCustomer: [
      { required: true, message: "总活跃客户数不能为空", trigger: "blur" }
    ],
    totalActiveSupplier: [
      { required: true, message: "总活跃供应商数不能为空", trigger: "blur" }
    ],
    totalCustomerOrders: [
      { required: true, message: "客户总订单数不能为空", trigger: "blur" }
    ],
    totalSupplierOrders: [
      { required: true, message: "供应商总订单数不能为空", trigger: "blur" }
    ],
    totalCustomerAmount: [
      { required: true, message: "客户总采购金额不能为空", trigger: "blur" }
    ],
    totalSupplierAmount: [
      { required: true, message: "供应商总采购金额不能为空", trigger: "blur" }
    ],
    createTime: [
      { required: true, message: "统计创建时间不能为空", trigger: "blur" }
    ],
    updateTime: [
      { required: true, message: "统计更新时间不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询月度统计信息-总列表 */
function getList() {
  loading.value = true;
  listScmStatsMonthly(queryParams.value).then(response => {
    scmStatsMonthlyList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    year: null,
    month: null,
    totalProjects: null,
    totalCustomer: null,
    totalSupplier: null,
    totalActiveCustomer: null,
    totalActiveSupplier: null,
    totalCustomerOrders: null,
    totalSupplierOrders: null,
    notOnTimeDeliveryOrders: null,
    onTimeDeliveryRate: null,
    totalCustomerAmount: null,
    totalSupplierAmount: null,
    totalCustomerQuantity: null,
    totalSupplierQuantity: null,
    totalCustomerDefectiveQuantity: null,
    totalSupplierDefectiveQuantity: null,
    totalStockIn: null,
    totalStockOut: null,
    productionYieldRate: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("scmStatsMonthlyRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加月度统计信息-总";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getScmStatsMonthly(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改月度统计信息-总";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["scmStatsMonthlyRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateScmStatsMonthly(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addScmStatsMonthly(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除月度统计信息-总编号为"' + _ids + '"的数据项？').then(function() {
    return delScmStatsMonthly(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('scm/scmStatsMonthly/export', {
    ...queryParams.value
  }, `scmStatsMonthly_${new Date().getTime()}.xlsx`)
}

getList();
</script>
